#include <retro.h>
#include "retro.creator.h"

using namespace Retro::Creator::Dialog;
using namespace SPLib::Windows;

VOID    EditSpriteMap::onUnload()
{
}

VOID    EditSpriteMap::onLoad()
{
  addHeader(RETRO_SPRITEMAP_MAXWIDTH, RETRO_SPRITEMAP_MAXHEIGHT);

  addFooter();
}

VOID    EditSpriteMap::newMap()
{
  TCHAR szName[RETRO_MAXTEXT];

  _stprintf(
    szName,
    _T("Sprite Map %.4d"),
    Frame::get()->getProject().getNextResourceID(Data::ResourceSpriteMap));

  SendMessage(hUpDownWidth, UDM_SETPOS32, 0, RETRO_SPRITEMAP_DEFWIDTH);
  SendMessage(hUpDownHeight, UDM_SETPOS32, 0, RETRO_SPRITEMAP_DEFHEIGHT);
  SendMessage(hUpDownLayers, UDM_SETPOS32, 0, RETRO_SPRITEMAP_DEFLAYERS);

  SetWindowText(hEditName, szName);
  setText(_T("New Sprite Map"));
}

VOID    EditSpriteMap::setMap(Data::SpriteMap* value)
{
  if (Frame::get()->getResource() == value) {
    Editor::MapEditor &editor = *Frame::get()->getSurface()->getEditor();

    SendMessage(hUpDownWidth, UDM_SETPOS32, 0, editor.getMapWidth());
    SendMessage(hUpDownHeight, UDM_SETPOS32, 0, editor.getMapHeight());
    SendMessage(hUpDownLayers, UDM_SETPOS32, 0, editor.getMapLayers());
  } else {
    SendMessage(hUpDownWidth, UDM_SETPOS32, 0, value->getWidth());
    SendMessage(hUpDownHeight, UDM_SETPOS32, 0, value->getHeight());
    SendMessage(hUpDownLayers, UDM_SETPOS32, 0, value->getLayers());
  }

  SetWindowText(hEditName, value->getName());
  setText(_T("Sprite Map Properties"));
}

VOID    EditSpriteMap::getMap(Data::SpriteMap* value)
{
  TCHAR szName[RETRO_MAXNAME];
  Edit_GetText(hEditName, szName, RETRO_MAXNAME);

  value->setName(szName);

  if (Frame::get()->getResource() == value) {
    Frame::get()->getSurface()->getEditor()->setBufferSize(
      (INT)SendMessage(hUpDownWidth, UDM_GETPOS32, 0, NULL),
      (INT)SendMessage(hUpDownHeight, UDM_GETPOS32, 0, NULL),
      (INT)SendMessage(hUpDownLayers, UDM_GETPOS32, 0, NULL));
  } else {
    value->setSize(
      (INT)SendMessage(hUpDownWidth, UDM_GETPOS32, 0, NULL),
      (INT)SendMessage(hUpDownHeight, UDM_GETPOS32, 0, NULL),
      (INT)SendMessage(hUpDownLayers, UDM_GETPOS32, 0, NULL));
  }
}
